package utility.text.mask;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/*
 * 
 * AJUSTAR para qualquer mascara 
 * Ex para telefone (verificar)
 * 
 */
public class MaskCustom extends PlainDocument {
	  
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int caracters = 0;  
  
    @Override  
    public void insertString(int offset, String str, AttributeSet attr) throws  
            BadLocationException {  
  
        if(str == null)  
            return;  
  
        if(caracters <= 12){  
  
            if(caracters == 0)  
                str = "("+str;  
            else if(caracters == 3)  
                str = ")"+str;  
            else if(caracters == 8)
            	str = "-"+str;  
  
            super.insertString(offset, str, attr);  
  
            caracters = getLength();  
  
        } else return;  
    }  
      
    @Override  
    public void remove(int offset, int length) throws BadLocationException {  
          
        super.remove(offset, length);  
          
        caracters = getLength();  
          
        if(caracters == 9 || caracters == 4 || caracters == 1){  
            super.remove(getLength()-1, 1);  
            caracters = getLength();  
        }  
    } 
}
